﻿using Library.Core.Application.BaseAction;
using Library.Core.Application.EventHandler;
using Library.Core.Application.IInfrastructure;
using Library.Core.Application.IInfrastructure.Event;
using Synthesize.Common.Domain.Event;
using Synthesize.Common.ServiceInterface.UseCase;
using System.Threading.Tasks;

namespace Synthesize.Common.Application.EventHandler
{
    /// <summary>
    /// 接口请求记录逻辑
    /// </summary>
    public class ApiLogEventHandler : BaseEventHandler<SafeApiMonitorLogEvent>, IEventHandler
    {

        private readonly EventErrorHandler _eventErrorHandler;
        private readonly ISafeService _safeService;

        public ApiLogEventHandler(IIocContainer container
             , EventErrorHandler eventErrorHandler
             , ISafeService safeService
            ) : base(container)
        {
            _eventErrorHandler = eventErrorHandler;
            _safeService = safeService;
        }

        /// <summary>
        /// 事件处理不支持泛型 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [EventHandler(MessageTopic.TopicName.ApiLog)]
        public override async Task Handle(SafeApiMonitorLogEvent input)
        {
            var re = await HandleAsync(input, async () =>
            {
                await _safeService.SaveApiMonitorLog(input);
                await _safeService.IncrementApiMonitorNumber(input);
                return true;
            });
            _eventErrorHandler.HandlerCustomError(re, "操作日志");
        }
    }
}
